﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class viewAllScholarships : System.Web.UI.Page
    {
        protected int currentPageNumber = 1;
        private const int PAGE_SIZE = 3;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }

        }


        private void BindData()
        {

            FundAL fundAL = new FundAL();
            List<ScholarshipDTO> allScholarship = new List<ScholarshipDTO>();
            List<ScholarshipDTO> allScholarshipOfOnePage = new List<ScholarshipDTO>();

            allScholarship = fundAL.GetAllScholarShip().OrderBy(a => a.Title).ToList();

            // get the total rows 
            double totalRows = (int)allScholarship.Count;

            lblAllScholarshipPage.Text = "Displaying all Scholarships offerered by the Colombo PPA / College - Page ";
            lblAllScholarshipOf.Text = "  of  ";

            lblAllScholarshipTotalPage.Text = CalculateTotalPages(totalRows).ToString();

            lblAllScholarshipCurrPage.Text = currentPageNumber.ToString();

            int totalPageNumber = lblAllScholarshipTotalPage.Text == "" ? 0 : Convert.ToInt32(lblAllScholarshipTotalPage.Text);

            if (totalPageNumber == 1)
            {
                if (currentPageNumber == 1)
                {
                    btnAllScholarshipFirst.Enabled = false;
                    btnAllScholarshipPrev.Enabled = false;
                    btnAllScholarshipNext.Enabled = false;
                    btnAllScholarshipLast.Enabled = false;
                    allScholarshipOfOnePage = allScholarship;
                }

            }
            else if (totalPageNumber > 1)
            {
                if (currentPageNumber == 1)
                {
                    btnAllScholarshipFirst.Enabled = false;
                    btnAllScholarshipPrev.Enabled = false;
                    btnAllScholarshipNext.Enabled = true;
                    btnAllScholarshipLast.Enabled = true;
                    allScholarshipOfOnePage = allScholarship.GetRange(0, PAGE_SIZE);
                }
                else if (currentPageNumber < totalPageNumber && currentPageNumber > 1)
                {
                    btnAllScholarshipFirst.Enabled = true;
                    btnAllScholarshipPrev.Enabled = true;
                    btnAllScholarshipNext.Enabled = true;
                    btnAllScholarshipLast.Enabled = true;
                    allScholarshipOfOnePage = allScholarship.GetRange(((currentPageNumber - 1) * PAGE_SIZE), PAGE_SIZE);

                }
                else if (currentPageNumber == totalPageNumber)
                {
                    btnAllScholarshipFirst.Enabled = true;
                    btnAllScholarshipPrev.Enabled = true;
                    btnAllScholarshipNext.Enabled = false;
                    btnAllScholarshipLast.Enabled = false;
                    allScholarshipOfOnePage = allScholarship.GetRange(((currentPageNumber - 1) * PAGE_SIZE), (allScholarship.Count - (currentPageNumber - 1) * PAGE_SIZE));
                }
            }
            


            gvViewAllScholarships.DataSource = allScholarshipOfOnePage;
            gvViewAllScholarships.DataBind();


        }

        private int CalculateTotalPages(double totalRows)
        {
            int totalPages = (int)Math.Ceiling(totalRows / PAGE_SIZE);

            return totalPages;
        }

        protected void ChangePage(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    currentPageNumber = 1;
                    break;

                case "Previous":
                    currentPageNumber = Int32.Parse(lblAllScholarshipCurrPage.Text) - 1;
                    break;

                case "Next":
                    currentPageNumber = Int32.Parse(lblAllScholarshipCurrPage.Text) + 1;
                    break;
                case "Last":
                    currentPageNumber = Int32.Parse(lblAllScholarshipTotalPage.Text);
                    break;
            }


            BindData();
        }
    }
}